package com.chuqiyun.authorization.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chuqiyun.authorization.annotation.AdminApiCheck;
import com.chuqiyun.authorization.entity.Commodity;
import com.chuqiyun.authorization.service.CommodityService;
import com.chuqiyun.authorization.utils.ResponseResult;
import com.chuqiyun.authorization.utils.exception.UnauthorizedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author mryunqi
 * @date 2023/4/19
 */
@RestController
public class SysCommodityController {
    @Value("${config.admin_path}")
    private String ADMIN_PATH;

    @Value("${config.secret}")
    private String secret;

    @Resource
    private CommodityService commodityService;

    /**
    * @Author: mryunqi
    * @Description: 添加商品
    * @DateTime: 2023/4/19 23:12
    * @Params: param JSONObject
    * @Return ResponseResult<String>
    */
    @AdminApiCheck
    @PostMapping("/{adminPath}/insertCommodity")
    public ResponseResult<String> insertCommodity(@PathVariable("adminPath") String adminPath,
                                                @RequestBody Commodity commodity) throws UnauthorizedException {
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }
        Commodity commodityInfo = commodityService.getCommodity(commodity.getName());
        if (!Objects.isNull(commodityInfo)){
            return ResponseResult.fail("该商品已存在");
        }
        if (commodityService.save(commodity)) {
            return ResponseResult.ok("添加成功！");
        } else {
            return ResponseResult.fail("添加失败！");
        }
    }

    /**
    * @Author: mryunqi
    * @Description: 获取指定id的商品
    * @DateTime: 2023/4/19 23:19
    * @Params: int id
    * @Return ResponseResult<Commodity>
    */
    @AdminApiCheck
    @GetMapping("/{adminPath}/getCommodityById")
    public ResponseResult<Commodity> getCommodityById(@PathVariable("adminPath") String adminPath,
                                               @RequestParam(value = "id") int id)
        throws UnauthorizedException{
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }
        Commodity commodityInfo = commodityService.getById(id);
        return ResponseResult.ok(commodityInfo);
    }

    /**
    * @Author: mryunqi
    * @Description: 获取指定名字的商品
    * @DateTime: 2023/4/19 23:23
    * @Params: String name 商品名
    * @Return ResponseResult<Commodity>
    */
    @AdminApiCheck
    @GetMapping("/{adminPath}/getCommodityByName")
    public ResponseResult<Commodity> getCommodityByName(@PathVariable("adminPath") String adminPath,
                                                      @RequestParam(value = "name") String name)
            throws UnauthorizedException{
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }
        Commodity commodityInfo = commodityService.getCommodity(name);
        return ResponseResult.ok(commodityInfo);
    }

    /**
    * @Author: mryunqi
    * @Description: 修改商品
    * @DateTime: 2023/4/19 23:31
    * @Params: Commodity
    * @Return ResponseResult<String>
    */
    @AdminApiCheck
    @PostMapping("/{adminPath}/updateCommodity")
    public ResponseResult<String> updateCommodity(@PathVariable("adminPath") String adminPath,
                                                  @RequestBody Commodity commodity)
        throws UnauthorizedException{
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }
        Commodity commodityInfo = commodityService.getById(commodity.getId());
        if (Objects.isNull(commodityInfo)){
            return ResponseResult.fail("该商品不存在");
        }
        if (commodityService.updateById(commodity)){
            return ResponseResult.ok("修改成功");
        }
        return ResponseResult.fail("修改失败");
    }

    @AdminApiCheck
    @GetMapping("/{adminPath}/getCommodityList")
    public ResponseResult<Page<Commodity>> getCommodityList(@PathVariable("adminPath") String adminPath,
                                                           @RequestParam(value = "page",defaultValue = "1") int page,
                                                           @RequestParam(value = "size",defaultValue = "20") int size)
            throws UnauthorizedException{
        if (!adminPath.equals(ADMIN_PATH)){
            //判断后台路径是否正确
            return ResponseResult.fail(ResponseResult.RespCode.NOT_PERMISSION);
        }
        return ResponseResult.ok(commodityService.getCommodityList(page, size));
    }
}
